package com.weitu.permission.roleAuthority.controller;

import com.weitu.base.annotaion.SystemLog;
import com.weitu.base.controller.BaseController;
import com.weitu.permission.role.entity.Role;
import com.weitu.permission.role.entity.RoleAuthority;
import com.weitu.permission.role.service.RoleService;
import com.weitu.permission.roleAuthority.service.RoleAuthorityService;
import com.weitu.util.AjaxResult;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author LiMeng 2017/3/21
 */
@Controller
@RequestMapping(value = "permission/roleAuthority")
public class RoleAuthorityController extends BaseController {
    private static final String MODULE_BATH_PATH = BASE_JSP_PATH + "permission/roleAuthority";
    private final RoleService roleService;
    private final RoleAuthorityService roleAuthorityService;

    @Autowired
    public RoleAuthorityController(RoleService roleService, RoleAuthorityService roleAuthorityService) {
        this.roleService = roleService;
        this.roleAuthorityService = roleAuthorityService;
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list() {
        return MODULE_BATH_PATH + "/list";
    }

    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String edit(Model model,String id) {
        Role role = roleService.queryRoleById(id);
        model.addAttribute(role);
        return MODULE_BATH_PATH + "/edit";
    }

    @SystemLog(module = "角色权限维护", methods = "角色授权")
    @ResponseBody
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public AjaxResult editAjax() {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        AjaxResult result = new AjaxResult();

        putParametersIntoMap(paramMap);

        String roleId = paramMap.get("roleId").toString();
        String authoritiesStr = paramMap.get("authoritiesStr").toString();

        if (StringUtils.isEmpty(authoritiesStr)){
            if (roleAuthorityService.deleteRoleAuthorityByRoleId(roleId)){
                result.setMessage("权限授予成功！");
                result.setSuccess(true);
                return result;
            }
            else {
                result.setMessage("权限授予失败！");
                return result;
            }
        }

        List<RoleAuthority> roleAuthorityList = getRoleAuthorityList(roleId, authoritiesStr);
        if (roleAuthorityService.updateRoleAuthority(roleId,roleAuthorityList)){
            result.setMessage("权限授予成功！");
            result.setSuccess(true);
            return result;
        }else {
            result.setMessage("权限授予失败！");
            return result;
        }
    }

    /**
     * 拼装用于插入的角色权限列表
     * @param roleId 角色id
     * @param authoritiesStr 权限字符串，格式: "权限id,权限2id"
     * @return 角色权限列表
     */
    private List<RoleAuthority> getRoleAuthorityList(String roleId, String authoritiesStr) {
        List<RoleAuthority> roleAuthorityList = new ArrayList<RoleAuthority>();
        String[] authorities = authoritiesStr.split(",");
        for (String authority : authorities) {
            RoleAuthority roleAuthority = new RoleAuthority();
            roleAuthority.setRoleId(roleId);
            roleAuthority.setAuthorityId(authority);
            roleAuthorityList.add(roleAuthority);
        }
        return roleAuthorityList;
    }
}
